package com.example.tracker;

import java.util.Collections;
import java.util.HashMap;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

public class DelegatingVehicleTracker {
	private static final String ERR_NO_SUCH_ID = "No such ID: ";

	private final ConcurrentMap<String, ImmutablePoint> locations;
	private final Map<String, ImmutablePoint> unmodifiableMap;

	public DelegatingVehicleTracker(Map<String, ImmutablePoint> data) {
		locations = new ConcurrentHashMap<>(data);
		unmodifiableMap = Collections.unmodifiableMap(locations);
	}

	public ImmutablePoint getLocation(String name) {
		ImmutablePoint loc = locations.get(name);
		if (loc == null) {
			throw new IllegalArgumentException(ERR_NO_SUCH_ID + name);
		}
		return loc;
	}

	public void setLocation(String name, ImmutablePoint point) {
		if (locations.replace(name, point) == null) {
			throw new IllegalArgumentException(ERR_NO_SUCH_ID + name);
		}
	}

	public Map<String, ImmutablePoint> getLocations() {
		return unmodifiableMap;
	}
	
	public Map<String, ImmutablePoint> getLocationsSnapshot() {
		return Collections.unmodifiableMap(new HashMap<>(locations));
	}
}
